package ryarithsGame;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;


import com.jme.bounding.BoundingSphere;
import com.jme.bounding.BoundingVolume;

import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;

import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;

public class BulletNode extends Node {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(BulletNode.class.getName());
	private float life;
	private boolean set;
	private Vector3f direction;

	
	private float speed;
	
	public float getLife() {
		return life;
	}
	public void setLife(float life) {
		this.life = life;
	}
	public boolean isSet() {
		return set;
	}
	public void setSet(boolean set) {
		this.set = set;
	}
	
	
	public BulletNode(String name, float spd)
	{
		super(name);
		loadShot();
		speed = spd;
		set = false;
	
		this.setLightCombineMode(LightCombineMode.Inherit);
		setLife(500);
		direction = new Vector3f();
		
	}
	public void Fire(Vector3f gunPos, Vector3f gunDir, Quaternion gunRot)
	{
		this.setLocalTranslation(gunPos);
		this.getChild(0).setLocalRotation(gunRot);
		set = true;
		life = 3f;
		direction.set(gunDir);
						
	}
	public void updateShot(float tpf)
	{
		this.setLocalTranslation(getLocalTranslation().add(direction.mult(speed * tpf)));
		life -= tpf;
		if(life < 0)
		{
			set = false;
		}
	}
	public int CheckCollision(BoundingVolume bv)
	{
		if(bv.contains(this.getLocalTranslation()))
		{
			set = false;
			return 1;
		}
		else
		{
			return 0;
		}
	}
	
	
	 private void loadShot()
	  {
		 URL model=BulletNode.class.getClassLoader().getResource("data/models/shot.3ds");

	        // Create something to convert .obj format to .jme
	        FormatConverter converter=new MaxToJme();
	        // Point the converter to where it will find the .mtl file from
	       // converter.setProperty("mtllib",model);

	        // This byte array will hold my .jme file
	        ByteArrayOutputStream BO=new ByteArrayOutputStream();
	        try {
	            // Use the format converter to convert .obj to .jme
	            converter.convert(model.openStream(), BO);
	            Node shot=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
	            // shrink this baby down some
	            shot.setLocalScale(2f);
	            shot.setModelBound(new BoundingSphere());
	            shot.updateModelBound();
	            // Put her on the scene graph
	            
	            
	            this.attachChild(shot);
	        } catch (IOException e) {
	        	//    Just in case anything happens
	            logger.logp(Level.SEVERE, this.getClass().toString(),
	                    "simpleInitGame()", "Exception", e);
	            System.exit(0);
	        }
	  }
}
